//
//  RHRetweetedView.swift
//  微博新浪
//
//  Created by hezerong on 16/6/13.
//  Copyright © 2016年 Rocky. All rights reserved.
//

import UIKit

class RHRetweetedView: UIView {

    //viewmodel
    var statusViewModel:RHStatusViewModel?{
    
        didSet{
        
              statusLabel.text = statusViewModel?.retweetedText
            
            //设置配图视图的数据模型
            pictureView.pic_urls = statusViewModel?.statusModel.retweeted_status?.pic_urls
            pictureView.viewModel  = statusViewModel
            
            
            //如果有有转发微博
            if let retweetedStatus = statusViewModel?.statusModel.retweeted_status{
            
                  //有图片
                if let pic_urls = retweetedStatus.pic_urls where pic_urls.count > 0{
                
                       statusLabel.snp_updateConstraints(closure: { (make) -> Void in
                        make.top.equalTo(self).offset(cellLayout_constant.separator)
                        make.left.equalTo(self).offset(cellLayout_constant.separator)
                        make.right.equalTo(self).offset(-cellLayout_constant.separator)
                        
                       })
                  
                    pictureView.snp_updateConstraints { (make) -> Void in
                        make.top.equalTo(statusLabel.snp_bottom).offset(cellLayout_constant.separator)
                        make.left.equalTo(statusLabel)
                        make.size.equalTo((statusViewModel?.retweetedPictureViewSize)!)
                        make.bottom.equalTo(self).offset(-cellLayout_constant.separator)
                    }

                    
                }
                    //没有图片
                else {
                    statusLabel.snp_updateConstraints { (make) -> Void in
                        make.top.equalTo(self).offset(cellLayout_constant.separator)
                        make.left.equalTo(self).offset(cellLayout_constant.separator)
                        make.right.equalTo(self).offset(-cellLayout_constant.separator)
                    }
                    
                    pictureView.snp_updateConstraints { (make) -> Void in
                        make.top.equalTo(statusLabel.snp_bottom)
                        make.left.equalTo(statusLabel)
                        make.size.equalTo(CGSizeZero)
                        make.bottom.equalTo(self).offset(-cellLayout_constant.separator)
                    }
                }
            }
                //如果没有转发微博
            else {
                statusLabel.snp_updateConstraints { (make) -> Void in
                    make.top.equalTo(self)
                    make.left.equalTo(self).offset(cellLayout_constant.separator)
                    make.right.equalTo(self).offset(-cellLayout_constant.separator)
                }
                
                pictureView.snp_updateConstraints { (make) -> Void in
                    make.top.equalTo(statusLabel.snp_bottom)
                    make.left.equalTo(statusLabel)
                    make.size.equalTo(CGSizeZero)
                    make.bottom.equalTo(self)
                }
            }
        }
    }
    
    //微博文字
    let statusLabel = UILabel(text: nil,  fontSize: 15)
    //配图视图
    let pictureView = RHPictureView()
    
    init(){
    
         super.init(frame: CGRectZero)
        
        setUpUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
  
}


extension RHRetweetedView{

    func setUpUI(){
    
       backgroundColor = UIColor.yellowColor()
        
        
        addSubview(statusLabel)
        addSubview(pictureView)
        
//        statusLabel.backgroundColor = UIColor.greenColor()
        
        statusLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.left.equalTo(self).offset(cellLayout_constant.separator)
            make.right.equalTo(self).offset(-cellLayout_constant.separator)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(statusLabel.snp_bottom).offset(cellLayout_constant.separator)
            make.left.equalTo(statusLabel)
            make.bottom.equalTo(self).offset(cellLayout_constant.separator)
            make.size.equalTo(CGSizeZero)
        }
    }
}